/**
 * 初始化页面
 */
$(function () {
    var id = sessionStorage.getItem('aId');
// console.log(id);
//发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: 'http://api.hk.com/admin/findById/'+id,
        success: function (data) {
            if (data.message == "success") {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#L_rpass").val(data.object.password);
                return false;
            }
            // console.log(data);
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }

    });
})

layui.use(['form', 'layer'],function() {
    $ = layui.jquery;
    var form = layui.form,
        layer = layui.layer;

    //自定义验证规则
    form.verify({
        formerPass: function (value) {
            if ($('#L_rpass').val() != $('#L_formerPass').val()) {
                return '旧密码输入错误';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        repass: function (value) {
            if ($('#L_pass').val() != $('#L_repass').val()) {
                return '两次密码不一致';
            }
        }
    });

    //监听提交
    form.on('submit(save)', function(data) {
        // console.log(data);
        //发异步，把数据提交给Java
        $.ajax({
            type: 'post',
            url: 'http://api.hk.com/admin/updatePassword',
            data: JSON.stringify(data.field),
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            success: function (res) {
                if (res.message == "success") {
                    $("#sumbit").attr("disabled",true); //禁止点击按钮
                    layer.msg('修改成功！', {icon: 1, time: 1100,end:function () {
                            //进行账号退出
                            $.get('/admin/destroyLogin',{},function (data) {
                                if (data.message == "success") {
                                    sessionStorage.removeItem("name");
                                    location.href='/html/admin/login.html';
                                    return false;
                                }
                                layui.layer.msg('退出失败，请稍后重试！',{icon:0,time:1000});
                                return false;
                            })
                        }});
                    return false;
                }
                layer.msg('修改失败！', {icon: 2, time: 1100});
                return false;
            },
            error: function () {
                layer.msg('请求接口异常！', {icon: 0, time: 1100});
                return false;
            }
        });
        return false;
    });
});

/**
 * 管理员修改密码表单重置
 */
function resetAdminPasswordFrom() {

    $("#L_formerPass").val("");
    $("#L_pass").val("");
    $("#L_repass").val("");

    var id = sessionStorage.getItem('aId');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: 'http://api.hk.com/admin/findById/'+id,
        success: function (data) {
            if (data.message == "success") {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#L_rpass").val(data.object.password);
                return  false;
            }
            // console.log(data);
            layer.msg('重置失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }

    });

}

// var _hmt = _hmt || []; (function() {
//     var hm = document.createElement("script");
//     hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
//     var s = document.getElementsByTagName("script")[0];
//     s.parentNode.insertBefore(hm, s);
// })();